package com.common.app.utls.imagepreviewer

import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target.SIZE_ORIGINAL
import com.common.app.R
import com.common.app.databinding.FragmentItemImagePreviewerBinding
import com.common.base.app.fragment.BaseVBFragment
import com.common.base.utils.StatusBarUtils

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/9/1
 * <p>
 * 描述：图片预览的fragment
 * <p>
 * 修订历史：
 */
class ImagePreviewerItemFragment : BaseVBFragment<FragmentItemImagePreviewerBinding>() {

    companion object {
        const val IMG_URL: String = "imgUrl"
    }

    override fun getViewBinding(): FragmentItemImagePreviewerBinding = FragmentItemImagePreviewerBinding.inflate(layoutInflater)

    override fun initViews() {
        super.initViews()
        StatusBarUtils.setStatusBarForegroundColor(activity, false)
        mViewBinding.mPvPic.maximumScale = 8.0f
    }

    override fun initEvents() {
        super.initEvents()
        mViewBinding.mPvPic.setOnPhotoTapListener { p0, p1, p2 -> activity?.finish() }
    }

    override fun lazyInitData() {
        super.lazyInitData()
        val imgUrl = arguments?.getString(IMG_URL)
        val options = RequestOptions()
            .centerCrop()
            .override(SIZE_ORIGINAL, SIZE_ORIGINAL)
            .placeholder(R.mipmap.icon_img_default)
            .error(R.mipmap.icon_img_default_error)

        activity?.let {
            val requestBuilder = Glide.with(it).load(imgUrl).apply(options)
            requestBuilder.transition(DrawableTransitionOptions.withCrossFade())
                .into(mViewBinding.mPvPic)
        }
    }
}